package com.kd.crawler.account.socialAccount;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONArray;
import com.kd.crawler.entity.Document;
import com.kd.crawler.entity.SocialAccountDoc;
import com.kd.crawler.jms.ActiveMQService;
import com.kd.crawler.utils.HttpRequestUtils;
import com.kd.crawler.utils.PropertiesUtils;

public class SocialAccountDataTransport {

	private static Logger logger = Logger.getLogger(SocialAccountDataTransport.class);
	
	public static void main(String[] args) {
		SocialAccountDataTransport sadt = new SocialAccountDataTransport();
		sadt.doTransport();
	}
	
	public void doTransport() {
		logger.debug("start to transport Social account data...");
		Properties pro = PropertiesUtils.getProperties("socialAccount.properties");
		String searchUrl = pro.getProperty("url.search");
		String updateUrl = pro.getProperty("url.update");
		int restTime = Integer.parseInt(pro.getProperty("rest.time", "180"));
		String typeIds = pro.getProperty("typeIds", "32,");
		String limit = pro.getProperty("limit", "20");
		Properties AMQPro = PropertiesUtils.getProperties("activeMQ.properties");
		String docMessageTopic = AMQPro.getProperty("QUEUE_CRAWL_DOC_MESSAGE");
		if(null==searchUrl || "".equals(searchUrl.trim())) {
			throw new RuntimeException("This url is null for Social account informant to search!");
		}
		if(null==updateUrl || "".equals(updateUrl.trim())) {
			throw new RuntimeException("This url is null for Social account informant to update!");
		}
		logger.info("transporting Social account data... ");
		Map<String, String> searchParams = new HashMap<String, String>();
		searchParams.put("typeIds", typeIds);
		searchParams.put("limit", limit);
		while(true) {
			JSONArray jsonArray = null;
			try {
				jsonArray = HttpRequestUtils.sendPostReturnJSONArray(searchUrl, searchParams);
			} catch(Exception e) {
				logger.error("failed to select some Social account data from database!", e);
			}
			if(null==jsonArray || jsonArray.isEmpty()) {
				try {
					System.out.println("There are no any anther Social account data now, Please wait for " + restTime + " seconds...");
					Thread.sleep(restTime*1000);
					continue ;
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			for(int i=0; i<jsonArray.size(); i++) {
				SocialAccountDoc socialAccount = jsonArray.getObject(i, SocialAccountDoc.class);
				Map<String, String> updateParams = new HashMap<String, String>();
				updateParams.put("url", socialAccount.getUrl());
				String title = socialAccount.getTitle();
				String content = socialAccount.getContent();
				if(null==content || "".equals(content.trim())) {
					content = socialAccount.getTitle();
					if(null==content || "".equals(content.trim())) {
						logger.error("The Social account document' title and content are null: " + socialAccount.logFor());
						HttpRequestUtils.sendPost(updateUrl, updateParams);
						continue ;
					} else {
						socialAccount.setContent(content.trim());
					}
				}
				if(null==title || "".equals(title.trim())) {
					title = content.length()<20? content : (content.substring(0, 20) + "...");
					socialAccount.setContent(title);
				}
				// reverse facebookDoc into Document
				Document doc = new Document();
				doc.setUrl(socialAccount.getUrl());
				doc.setTitle(socialAccount.getTitle());
				doc.setAuthor(socialAccount.getAuthor());
				doc.setDate(socialAccount.getDate());
				doc.setContent(socialAccount.getContent());
				doc.setClickNum(socialAccount.getClickNum());
				doc.setCommentNum(socialAccount.getCommentNum());
				doc.setJobId(socialAccount.getSource());
				doc.setSource(socialAccount.getSource());
				doc.setType(socialAccount.getType());
				doc.setLevel(socialAccount.getLevel());
//				doc.setLevel(2);
//				doc.setUrl(doc.getUrl()+"#"+MD5Utils.MD5(doc.getContent().trim()));
				logger.info("start to enqueue to JMS for the document: " + doc.logFor());
				// send a document to JMS
				if(ActiveMQService.sendTopic(docMessageTopic, doc)) {
					System.out.println(HttpRequestUtils.sendPost(updateUrl, updateParams));
					logger.info("Success to enqueue to JMS for the document: " + doc.logFor());
				} else {
					logger.warn("Failed to enqueue to JMS for the document: " + doc.logFor());
				}
			}
		}
	}
}
